package com.banyuan.dao;

import com.banyuan.domain.Opus;
import com.banyuan.utils.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;

public class OpusDao implements PageDao {



    //通过作者的id查询这个作者的所有作品的信息

    public static ResultSet selectAll(String sql,String limit,Object ...objects) throws SQLException {
        System.out.println(sql);
        ResultSet resultSet = JDBCUtils.executeQuery("select opus.id,opus.icon,opus.name,opus.typeId,types.name typeName,opus.authorId,author.userName userName,opus.intro,opus.createTime,opus.updateTime\n" +
                "from opus,types,author where " +
                " opus.authorId=author.id\n" +
                "and opus.typeId=types.id "+ sql + " order by opus.id desc " + limit, objects);
        return resultSet;
    }

    // 添加一条信息，

    public static int insert(Opus opus) throws SQLException {
        int i = JDBCUtils.executeUpdate("insert into opus(icon,name,typeId,authorId,intro,createTime,updateTime) values(?,?,?,?,?,now(),now())",
                opus.getIcon(), opus.getName(), opus.getTypeId(), opus.getAuthorId(), opus.getIntro());
        return i;

    }

    // 通过书本id去查处书本的所有信息

    public static ResultSet selectOpusById(Integer id) throws SQLException {
        ResultSet resultSet = JDBCUtils.executeQuery("select * from opus where id = ?", id);
        return resultSet;
    }

    // 通过书本id去删除一个书本的所有信息
    public static int deleteById(Integer id) throws SQLException {
        int i = JDBCUtils.executeUpdate("delete from opus where id = ?", id);
        return i;
    }

    // // 修改一条信息
    public static int update(Opus opus) throws SQLException {
        int i = JDBCUtils.executeUpdate("update opus set icon=?,name=?,typeId=?,intro=?,updateTime=now() where id = ?",
                opus.getIcon(), opus.getName(), opus.getTypeId(), opus.getIntro(),opus.getId());
        return i;


    }

    @Override
    public ResultSet selectCountAll(String sql, Object... objects) throws SQLException {
        ResultSet resultSet = JDBCUtils.executeQuery("select count(*" +
                ") " +
                "from opus,types,author " +
                "where opus.authorId=author.id\n" +
                "and opus.typeId=types.id" + sql +  " order by opus.id desc", objects);
        return resultSet;
    }
}
